/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.edu.amu.wmi.kino.visualjavafx.javafxcodeparsers.objects;

import pl.edu.amu.wmi.kino.visualjavafx.javafxcodeparsers.api.Parser;
import pl.edu.amu.wmi.kino.visualjavafx.javafxcodeparsers.main.MainParser;
import pl.edu.amu.wmi.kino.visualjavafx.javafxcodeparsers.objects.ApplicationParser;
import pl.edu.amu.wmi.kino.visualjavafx.javafxcodeparsers.tools.CanParseMatcher;

/**
 *
 * @author Admin
 */
public class ImportParser implements Parser{
    private static final String CAN_PARSE_REGEXP = "^import\\s+(\\w+\\.{1})+[\\w[*]]+";
    private static final String EXTRACT_REGEXP = "\\s+(\\w+\\.{1})+[\\w[*]]+";

    @Override
    public boolean canParse(String s) {
        return CanParseMatcher.matches(s, CAN_PARSE_REGEXP);
    }

    @Override
    @SuppressWarnings("static-access")
    public Object parse(Object instructions, Object parent) {
         //TODO : Grzegorz : unnecesary if only ApplicationParser can use it
         ApplicationParser parentParser = null;
         String s = null;
         
         if(parent instanceof ApplicationParser){
              parentParser = (ApplicationParser)parent;
         }else{
             throw new UnsupportedOperationException("ImportParser executed in improper context!");
         }
         if(instructions instanceof String){
            s = (String)instructions;
         }else{
             throw new UnsupportedOperationException("ImportParser given bad parsingData!");
         }
         
         String res = CanParseMatcher.extractMatch(s, this.EXTRACT_REGEXP);
         parentParser.getImportManager().getImports().add(res);

         return parentParser;
         
    }

    @Override
    public boolean getProperContext(Class c) {
        //TODO : Grzegorz : Change this method when all known properContexts are known
        if(c==MainParser.class)
            return true;
        else return false;
    }


}
